package com.sky.service.impl;

import com.github.pagehelper.Constant;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.exception.SetmealEnableFailedException;
import com.sky.mapper.SetDishMapper;
import com.sky.mapper.SetMealDishMapper;
import com.sky.result.PageResult;
import com.sky.service.SetMealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;

@Service
public class SetMealServiceImpl implements SetMealService {
    @Autowired
    private SetDishMapper setDishMapper;

    @Autowired
    private SetMealDishMapper setMealDishMapper;

    /**
     * 新增套餐 套餐里面有菜品
     * @param setmealDTO
     */
    @Transactional (rollbackFor = Exception.class)
    @Override
    public void save(SetmealDTO setmealDTO) {
        //新增套餐
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setDishMapper.insert(setmeal);
        //新增套餐关联的菜品
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        setmealDishes.forEach(setmealDish -> {
            setmealDish.setSetmealId(setmeal.getId());
        });
        setMealDishMapper.insertBatch(setmealDishes);
    }

    /**
     * 套餐分页查询
     * @param setmealPageQueryDTO
     * @return
     */
    @Override
    public PageResult page(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());
        Page<SetmealVO> page = setDishMapper.page(setmealPageQueryDTO);
        return new PageResult(page.getTotal(),page.getResult());
    }


    /**
     * 批量删除套餐
     * @param ids
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void deleteBatchByid(List<Long> ids) {
        //如果是启售状态 则不允许删除
        for (Long id : ids) {
            Setmeal setmeal = setDishMapper.getByid(id);
            if(setmeal.getStatus() == StatusConstant.ENABLE){
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
        }

        //删除套餐
        setDishMapper.deleteBatchByid(ids);
        //删除套餐关联的菜品
        setMealDishMapper.deleteBatchBysetdishId(ids);
    }

    /**
     * 根据id查询套餐信息
     * @param id
     * @return
     */
    @Override
    public SetmealVO getSetmealwithDish(Long id) {
        //先要查询到套餐的信息
        SetmealVO setWithCategoryName = setDishMapper.getSetWithCategoryName(id);
        //然后要查询到套餐关联表的菜品信息
        List<SetmealDish> byid = setMealDishMapper.getByid(id);
        setWithCategoryName.setSetmealDishes(byid);
        return setWithCategoryName;
    }

    /**
     * 修改套餐的信息
     * @param setmealDTO
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void update(SetmealDTO setmealDTO) {
        //先修改套餐的信息
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setDishMapper.updateByid(setmeal);
        //接着删除套餐关联的菜品
        setMealDishMapper.deleteBatchBysetdishId(Collections.singletonList(setmealDTO.getId()));
        //再进行套餐关联菜品的添加即可
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        setmealDishes.forEach(setmealDish -> {
            setmealDish.setSetmealId(setmeal.getId());
        });
        setMealDishMapper.insertBatch(setmealDishes);
    }

    /**
     * 启售停售
     * @param status
     * @param id
     */
    @Override
    public void startOrStop(Integer status, Long id) {
        //如果套餐内有停止售的菜品 则不让启售
        if(status == StatusConstant.ENABLE){
            //如果要启售
            //通过套餐菜品关联表来查找菜品
            List<Dish> dishes = setMealDishMapper.getDishbySetId(id);
            for (Dish dish : dishes) {
                if(dish.getStatus() == StatusConstant.DISABLE){
                    throw new SetmealEnableFailedException(MessageConstant.SETMEAL_ENABLE_FAILED);
                }
        }
        }
        Setmeal setmeal = Setmeal.builder().id(id).status(status).build();
        setDishMapper.updateByid(setmeal);
    }

    /**
     * 根据分类查找套餐
     * @param categoryId
     * @return
     */
    @Override
    public List<Setmeal> list(Long categoryId) {
        Setmeal setmeal = new Setmeal();
        //来做一个动态查询
        setmeal.setCategoryId(categoryId);
        setmeal.setStatus(StatusConstant.ENABLE);
        return setDishMapper.list(setmeal);
    }

    /**
     * 根据套餐id来查找菜品 包括了份数
     * @param id
     * @return
     */
    @Override
    public List<DishItemVO> getDishBySetmealId(Long id) {
        return setMealDishMapper.getDishbySetmealId(id);
    }


}
